package com.ttmall.controller;

import com.ttmall.common.pojo.KindeditorPicUploadResult;
import com.ttmall.service.PictureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * Created by GalaIO on 2018/1/14.
 */
@Controller
public class MediaFileController {

    @Autowired
    private PictureService pictureService;

    @RequestMapping("/pic/upload")
    @ResponseBody
    private KindeditorPicUploadResult uploadItemPic(MultipartFile uploadFile) {
        return pictureService.uploadItemPic(uploadFile);
    }

    @RequestMapping("/images/**")
    @ResponseBody
    private ResponseEntity<byte[]> getImages(HttpServletRequest request) throws NoHandlerFoundException {
        try {
            return pictureService.fetchFile(request.getRequestURI());
        } catch (IOException e) {
            throw new NoHandlerFoundException(request.getMethod(), request.getRequestURI(), null);
        }

    }
}
